package com.mt.system.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.system.entity.LinkEntity;
import com.mt.system.mapper.LinkEntityMapper;

@Component
public class LinkServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(LinkServiceModel.class);

    @Autowired
    private LinkEntityMapper linkEntityMapper;

    public ServiceResult<List<LinkEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.linkEntityMapper, "Property 'linkEntityMapper' is required.");
        ServiceResult<List<LinkEntity>> result = new ServiceResult<List<LinkEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.linkEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[SupplierImageServiceImpl][findList]：query findList occur exception", e);
        }
        return result;
    }

    public ServiceResult<LinkEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.linkEntityMapper, "Property 'linkEntityMapper' is required.");
        ServiceResult<LinkEntity> result = new ServiceResult<LinkEntity>();
        LinkEntity linkEntity = null;
        try {
            linkEntity = this.linkEntityMapper.selectByPrimaryKey(params);
            result.setResult(linkEntity);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[LinkServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(LinkEntity linkEntity) {
        Assert.notNull(this.linkEntityMapper, "Property 'linkEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.linkEntityMapper.insert(linkEntity);
            if (save > 0) {
                id = linkEntity.getLinkId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[LinkServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(LinkEntity linkEntity) {
        Assert.notNull(this.linkEntityMapper, "Property 'linkEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.linkEntityMapper.updateByPrimaryKeySelective(linkEntity);
            if (save > 0) {
                id = linkEntity.getLinkId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[LinkServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer linkId) {
        Assert.notNull(this.linkEntityMapper, "Property 'linkEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            int save = this.linkEntityMapper.deleteByPrimaryKey(linkId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[LinkServiceModel][deleteByPrimaryKey]:query update occur exception", e);
        }
        return result;
    }
}
